#ifndef __WEB_SOCKET_H__
#define __WEB_SOCKET_H__
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <boost/thread/locks.hpp>  
#include <boost/thread/shared_mutex.hpp> 
#include <boost/thread/thread.hpp>

#include <map>
#include "redis_subscriber.h"
#include "httpclient.h"

typedef websocketpp::server<websocketpp::config::asio> server;
typedef struct MessageOpenId {
    MessageOpenId(server::message_ptr message, std::string &openId)
    :m_message(message), m_openId(openId)
    {

    }
    server::message_ptr m_message;
    std::string m_openId;
}MessageOpenId;

class CWebSocket
{
public:
    CWebSocket(CRedisSubscriber &subscriber);
    ~CWebSocket();
public:    
public:
    void run(boost::uint16_t port);
    void send(const std::string &openId, const std::string &message);
private:
    void on_open(websocketpp::connection_hdl hdl);
    void on_close(websocketpp::connection_hdl hdl);
    void on_http(websocketpp::connection_hdl hdl);
    void on_message(websocketpp::connection_hdl hdl, server::message_ptr msg);
    void process_messages();
    void send_html(server::connection_ptr &con, const std::string &content);
private:    
    typedef std::map<std::string, websocketpp::connection_hdl> ConMap;
    CRedisSubscriber &m_subscriber;

    server m_server;
    ConMap m_conMap;
    boost::shared_mutex m_conMapWrMutex;

    CHttpPara m_httpPara;
    boost::thread m_messageThread;
    std::queue<MessageOpenId> m_messageQueue;  
    boost::mutex m_messageLock;  
    boost::condition_variable m_messageCond;
};
#endif

